<?php
require_once 'Zend/Controller/Action.php';
class Administrator_StatisticsController extends Zend_Controller_Action
{
	public function init()
    {
        $member=new CMember();
        if(!$member->isUser($this->_request->getModuleName(),$this->_request->getControllerName()))
        {
            $this->_redirect(MY_URL.'/administrator-auth-login');
        }
    	$memberArray = $member->getUserDetails(array('uid' => $_COOKIE[COOKIE_PRE.'uid']));
    	if($_COOKIE[COOKIE_PRE.'uid']=='1'){
		} else {
        	$strarray = explode(",", $memberArray[0]['menuflag']);
        	if (!in_array('6', $strarray)) {
        		exit('no priviage');
        	}
		}
    }

    public function preDispatch()
    {
        $this->getHelper ('layout')->setLayout('framelayout');
        $this->view->addHelperPath(MY_ROOT.'/vhelpers');
        Zend_Controller_Action_HelperBroker::addPath(MY_ROOT.'/chelpers');
    }
    
    /*
     * 第一点
     */
    public function countryAction() {
		$params['locationid'] = '';
		$params['timestart'] = '';
		$params['timeend'] = '';
		$locationid = $this->getRequest ()->getParam ('locationid','');
		$timestart = $this->getRequest ()->getParam ('timestart','');
		$timeend = $this->getRequest ()->getParam ('timeend','');
		if($locationid) {
			$params['locationid'] = $locationid;
		} else if($this->getRequest()->isPost()) {
			echo '<script>alert("请选择国家");</script>';
		}
		if($timestart) {
			$params['timestart'] = $timestart;
		} else if($this->getRequest()->isPost()) {
			echo '<script>alert("请输入开始时间");</script>';
		}
		if($timeend) {
			$params['timeend'] = $timeend;
		} else if($this->getRequest()->isPost()) {
			echo '<script>alert("请输入结束时间");</script>';
		}
		//地区信息
		$location=new CFenlei('location');
		$location->setOrder('en_flname ASC');
		$fenleiArray=$location->getFenleiByPid(0);
		$this->view->assign('fenleiArray',$fenleiArray);
		//地区信息
		if($locationid && $timestart && $timeend) {
			
			$statistics = new CStatistics();
			$result = $statistics->unionLocation($page, $page_size, $params, 'country');
			
	        $this->view->total       = $result ['total'];
		}
        $this->view->params = $params;
    }
    
    
    /*
     * 第二点
     */
	public function cityAction() {
		$params['city'] = '';
		$params['timestart'] = '';
		$params['timeend'] = '';
		$city = $this->getRequest ()->getParam ('city','');
		$timestart = $this->getRequest ()->getParam ('timestart','');
		$timeend = $this->getRequest ()->getParam ('timeend','');
		if($city) {
			$params['city'] = $city;
		} else if($this->getRequest()->isPost()) {
			echo '<script>alert("请输入城市");</script>';
		}
		if($timestart) {
			$params['timestart'] = $timestart;
		} else if($this->getRequest()->isPost()) {
			echo '<script>alert("请输入开始时间");</script>';
		}
		if($timeend) {
			$params['timeend'] = $timeend;
		} else if($this->getRequest()->isPost()) {
			echo '<script>alert("请输入结束时间");</script>';
		}
		
		if($city && $timestart && $timeend) {
			$statistics = new CStatistics();
			$result = $statistics->unionLocation($page, $page_size, $params, 'city');
			
	        $this->view->total       = $result ['total'];
		}
        $this->view->params = $params;
	}
	
    
    /*
     * 第三点
     */
	public function memberAction() {
		$params['email'] = '';
		$params['timestart'] = '';
		$params['timeend'] = '';
		$email = $this->getRequest ()->getParam ('email','');
		$timestart = $this->getRequest ()->getParam ('timestart','');
		$timeend = $this->getRequest ()->getParam ('timeend','');
		if($email) {
			$params['email'] = $email;
		} else if($this->getRequest()->isPost()) {
			echo '<script>alert("请输入用户EMAIL");</script>';
		}
		if($timestart) {
			$params['timestart'] = $timestart;
		} else if($this->getRequest()->isPost()) {
			echo '<script>alert("请输入开始时间");</script>';
		}
		if($timeend) {
			$params['timeend'] = $timeend;
		} else if($this->getRequest()->isPost()) {
			echo '<script>alert("请输入结束时间");</script>';
		}
		
		if($email && $timestart && $timeend) {
			$page = ($this->getRequest()->get('page')) ? $this->getRequest()->get('page') : 1;
			$page_size = 10;

			$statistics = new CStatistics();
			$result = $statistics->unionUser($page, $page_size, $params);
			
			$this->view->page       = $result ['page'];
	        $this->view->list       = $result ['list'];
	        $this->view->total       = $result ['total'];
		}
        $this->view->params = $params;
	}
	
    
    /*
     * 第五点
     */
	public function orderdetailAction() 
	{
		$orderfl=new CFenlei('order');
        $fenleiArray=$orderfl->getAllFenlei();
        $this->view->assign('fenleiArray',$fenleiArray);
        
        $beginTime = $this->getRequest()->getParam('begintime', date('Y-m-d', time() - 3600*24*30));
        $endTime = $this->getRequest()->getParam('endtime', date('Y-m-d', time()));
        $flid = $this->getRequest()->getParam('flid', 0);
        $page = $this->getRequest()->getParam('page', 1);
        $flid = intval($flid);
        $page = intval($page);
        
        $pageNum = 50;
        
        $statistics = new CStatistics();
        $result = $statistics->getOrders($beginTime, $endTime, $flid);
		
		$product = $result['product'];
		uasort($product,array('CStatistics','sortBySellNum'));
        
        $param = 'administrator-statistics-orderdetail?begintime=' . $beginTime . '&endtime=' . $endTime . '&flid=' . $flid . '&';
        $pageLink = $statistics->orderPage(count($product), $pageNum, $page, $param);
        
        $offset = ($page - 1) * $pageNum;
        
        $keyArray = array_keys($product);
        $valueArray = array_values($product);
        
        $keys = array_slice($keyArray, $offset, $pageNum);
        $values = array_slice($valueArray, $offset, $pageNum);
        
        $products = array();
        
        for($i=0;$i<count($keys); $i++) {
        	$products[$keys[$i]] = $values[$i];
        }
        
        $this->view->products = $products;
        $this->view->page = $pageLink;
        $this->view->pageLink = $pageLink;
        $this->view->beginTime = $beginTime;
        $this->view->endTime = $endTime;
        $this->view->flid = $flid;
        $this->view->result = $result;
        
        $item = new CItem();
        $this->view->item = $item;
	}
	
    
    /*
     * 第六点
     */
	public function buyrankAction() {
		$params['orderby'] = '';
		$orderby = $this->getRequest ()->getParam ('orderby','');
		$beginTime = $this->getRequest()->getParam('begintime', date('Y-m-d', time() - 3600*24*30));
        $endTime = $this->getRequest()->getParam('endtime', date('Y-m-d', time()));
        $params['begintime'] = $beginTime;
        $params['endtime'] = $endTime;
		if($orderby) {
			$params['orderby'] = $orderby;
		} else if($this->getRequest()->isPost()) {
			echo '<script>alert("请选择排名方式");</script>';
		}
		
		if($orderby) {
			$page = ($this->getRequest()->get('page')) ? $this->getRequest()->get('page') : 1;
			$page_size = 20;

			$statistics = new CStatistics();
			$result = $statistics->unionUserRank($page, $page_size, $params);
			
			$this->view->page       = $result ['page'];
	        $this->view->list       = $result ['list'];
		}
        $this->view->params = $params;
	}
	
    
    /*
     * 导出EXCEL
     */
	public function exportexcelAction() {
		
	}
    
    /*
     * 导出EXCEL
     */
	public function doexportexcelAction() {
		$year = $this->getRequest ()->getParam ('year','');
		$month = $this->getRequest ()->getParam ('month','');
		$begintime = $year.'-'.str_pad($month, 2, "0", STR_PAD_LEFT).'-01';
		if($month == 1) {
			$endtime = ($year-1).'-12-01';
		} else {
			$endtime = $year.'-'.str_pad($month-1, 2, "0", STR_PAD_LEFT).'-01';
		}
		$params['timestart'] = $endtime;
		$params['timeend'] = $begintime;
        
		$statistics = new CStatistics();
		$result = $statistics->membermonth($page, $page_size, $params);
//		var_dump(unserialize($result['list'][0]['order_string']));exit;
		
		$this->view->year		= $year;
		$this->view->month		= $month;
        $this->view->list       = $result ['list'];
	}
	
}